package cn.edu.swust.adressbook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.edu.swust.adressbook.R;
import cn.edu.swust.adressbook.bean.Group;

/**
 * @file 群组选择适配器
 * 此类是群组选择适配器，作为群组选择listView的一部分
 */

public class GroupSelectItemAdapter extends BaseAdapter {

    private List<Group> mGroups; ///< 群组链表
    private Context mContext;    ///< listView上下文
    private LayoutInflater mLayoutInflater;

    /**
     * 群组选择适配器构造函数 主要初始化上下文和LayoutInflater
     * @param context listView上下文
     */
    public GroupSelectItemAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 初始化群组链表
     * @param groups 新的群组链表
     */
    public void setGroups(List<Group> groups) {
        mGroups = groups;
    }

    @Override
    public int getCount() {
        if (mGroups == null) {
            return 0;
        }
        return mGroups.size();
    }

    @Override
    public Object getItem(int i) {
        return mGroups.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView textView;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.activity_select_group_item, null);
            textView = (TextView) view.findViewById(R.id.group_item_name);
            view.setTag(textView);
        } else {
            textView = (TextView) view.getTag();
        }
        textView.setText(mGroups.get(i).getName());
        return view;
    }
}
